package cn.iocoder.yudao.module.wengfu.framework.excel.core.util;

import cn.idev.excel.write.handler.WorkbookWriteHandler;
import cn.idev.excel.write.handler.context.WorkbookWriteHandlerContext;
import org.apache.poi.ss.usermodel.*;

public class FormulaCalculateWriteHandler implements WorkbookWriteHandler {

    @Override
    public void afterWorkbookCreate(WorkbookWriteHandlerContext context) {
        // 在所有数据写入完成后计算公式
    }

    @Override
    public void afterWorkbookDispose(WorkbookWriteHandlerContext context) {
        try {
            Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
            FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
            evaluator.evaluateAll();
            System.out.println("公式计算完成");
        } catch (Exception e) {
            System.err.println("计算公式时发生错误: " + e.getMessage());
        }
    }
}
